package com.baidu.elinkagescroll.sample;

import com.baidu.elinkagescroll.ELinkageScrollLayout;
import com.baidu.elinkagescroll.ResourceTable;
import com.baidu.elinkagescroll.utils.ToastUtil;
import com.baidu.elinkagescroll.view.LRecyclerView;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Button;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.VectorElement;

import java.util.ArrayList;

/**
 * MainAbility
 *
 * @since 2021/7/15
 */
public class MainAbility extends Ability {
    private LRecyclerView mRecyclerView;
    private ELinkageScrollLayout mElink;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_colorPrimaryDark));
        initView();
    }

    private void initView() {
        mElink = (ELinkageScrollLayout) findComponentById(ResourceTable.Id_elink);

        mRecyclerView = (LRecyclerView) findComponentById(ResourceTable.Id_recycler1);
        LinkageRecyclerAdapter adapter = new LinkageRecyclerAdapter(this, getRVData(20), "#6600ffff");
        mRecyclerView.setItemProvider(adapter);

        mRecyclerView = (LRecyclerView) findComponentById(ResourceTable.Id_recycler2);
        LinkageRecyclerAdapter adapter2 = new LinkageRecyclerAdapter(this, getRVData(5), "#66ff00ff");
        mRecyclerView.setItemProvider(adapter2);

        mRecyclerView = (LRecyclerView) findComponentById(ResourceTable.Id_recycler_in_frame_layout);
        LinkageRecyclerAdapter adapter3 = new LinkageRecyclerAdapter(this, getRVInFrameData(20), "#66ff0000");
        mRecyclerView.setItemProvider(adapter3);

        Button button = (Button) findComponentById(ResourceTable.Id_button);
        Element element = ElementScatter.getInstance(this).parse(ResourceTable.Graphic_btn_ripple_bg);
        button.setBackground(getButtonsRippleColor(element));
        button.setClickedListener(component -> {
            ToastUtil.show(MainAbility.this, "Button Click");
        });

        Button gotoTargetBtn = (Button) findComponentById(ResourceTable.Id_btn_go_to_target);
        Element element2 = ElementScatter.getInstance(this).parse(ResourceTable.Graphic_btn_ripple_bg);
        gotoTargetBtn.setBackground(getButtonsRippleColor(element2));
        gotoTargetBtn.setClickedListener(component -> {
            mElink.gotoChild(2);
        });
    }

    private Element getButtonsRippleColor(Element element) {
        Element resultElement = null;
        if (element instanceof PixelMapElement) {
            resultElement = (PixelMapElement) element;
        } else if (element instanceof VectorElement) {
            resultElement = (VectorElement) element;
        } else {
            resultElement = element;
        }
        return resultElement;
    }

    private ArrayList<String> getRVData(int count) {
        ArrayList<String> data = new ArrayList<>();
        String temp = "RecyclerView - ";
        for (int i = 0; i < count; i++) {
            data.add(temp + i);
        }
        return data;
    }

    private ArrayList<String> getRVInFrameData(int count) {
        ArrayList<String> data = new ArrayList<>();
        String temp = "RecyclerInFrameLayout - ";
        for (int i = 0; i < count; i++) {
            data.add(temp + i);
        }
        return data;
    }
}
